package com.ruoyi.common.utils.im;

import cn.jiguang.common.resp.APIConnectionException;
import cn.jiguang.common.resp.APIRequestException;
import cn.jmessage.api.JMessageClient;
import cn.jmessage.api.common.model.RegisterInfo;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.model.IMUserModel;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.spring.SpringUtils;
import org.springframework.web.bind.annotation.RequestBody;

/**
 * @version :
 * @program: easyBuy
 * @description:
 * @author: 周爱鸿
 * @create: 2020-11-02 14:23
 **/
public class IMUtils {

	public static final String SUCCESS = "success";
	public static final String FAIL = "fail";

	public static String registerUser(IMUserModel[] users){

		JMessageClient jMessageClient = SpringUtils.getBean(JMessageClient.class);
		String result = "";
		try {

			RegisterInfo[] registerInfos = new RegisterInfo[users.length];
			for (int i = 0 ; i< users.length ; i++) {
				IMUserModel user = users[i];
				registerInfos[i]
						= RegisterInfo.newBuilder()
						.setUsername(user.getUsername())
						.setPassword(user.getPassword())
						.setNickname(user.getNickname())
						.setAvatar(user.getAvatar())
						.setGender(user.getGender())
						.setBirthday(user.getBirthday())
						.setAddress(user.getAddress())
						.setRegion(user.getRegion())
						.setSignature(user.getSignature())
						.build();
			}

			result = jMessageClient.registerUsers(registerInfos);
		} catch (APIConnectionException e) {
			e.printStackTrace();
			return FAIL;
		} catch (APIRequestException e) {
			e.printStackTrace();
			return FAIL;
		}
		return SUCCESS;
	}

}
